package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbCatagory;
import com.beetlestech.nfe.main.biz.com.dto.CodeWithValueDTO;
import com.beetlestech.nfe.main.biz.com.dto.MstcatagoryInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant.CATAGORY_P_MESSAGE_STATE;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.CoursesectionDTO;
import com.beetlestech.nfe.main.db.tbl.MstcatagoryDTO;
import com.beetlestech.nfe.main.db.tbl.MstcatagorycDTO;
import com.beetlestech.nfe.main.db.tbl.MstcourseDTO;
import com.beetlestech.nfe.main.db.tbl.SectionlessonDTO;
import com.beetlestech.nfe.main.db.tbl.UsermessageDTO;
import com.beetlestech.nfe.main.db.view.VsectionidfordeleteteacherDTO;
import com.sun.org.apache.regexp.internal.recompile;

public class HbCatagoryImpl extends AbstractDBLogic
		implements HbCatagory, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "课程目录";

	@Override
	public CodeWithValueDTO[] getCourseTypeCatagory() {
		MstcatagorycDTO searchDTO = new MstcatagorycDTO();
		List<CodeWithValueDTO> codeArray = new ArrayList<CodeWithValueDTO>();
		searchDTO.setcatagorypid(Integer.parseInt(CATAGORY_P_COURESE_TYPE.OWNER.toString()));
		List<MstcatagorycDTO> catagorys = queryDAO.queryForObjectList(MstcatagorycDTO.EQUAL_SEARCH, searchDTO);
		for (MstcatagorycDTO item : catagorys) {
			CodeWithValueDTO codeDTO = new CodeWithValueDTO();
			codeDTO.setCode(item.getcatagorycid().toString());
			codeDTO.setValue(item.getcatagorycname());
			codeArray.add(codeDTO);
		}
		return codeArray.toArray(new CodeWithValueDTO[codeArray.size()]);
	}

	@Override
	public boolean createCatagory(String name, Integer level, Integer orders, Integer fatherid) {
		MstcatagoryDTO catagoryDTO = new MstcatagoryDTO();
		catagoryDTO.setName(name);
		catagoryDTO.setFatherid(fatherid);
		List<MstcatagoryDTO> catagorys = queryDAO.queryForObjectList(MstcatagoryDTO.EQUAL_SEARCH, catagoryDTO);
		if (catagorys.size() > 0) {
			return false;
		} else {
			catagoryDTO.setLevel(level);
			catagoryDTO.setOrders(orders);
			updateDAO.updateOne(MstcatagoryDTO.INSERT, catagoryDTO, M10190WS, SH_INFO);
			return true;
		}

	}

	@Override
	public MstcatagoryInfoDTO[] getAllCatagory() {

		MstcatagoryDTO searchDTO = new MstcatagoryDTO();
		List<MstcatagoryInfoDTO> catagoryArray = new ArrayList<MstcatagoryInfoDTO>();
		List<MstcatagoryDTO> catagorys = queryDAO.queryForObjectList(MstcatagoryDTO.EQUAL_SEARCH, searchDTO);
		for (MstcatagoryDTO item : catagorys) {
			MstcatagoryInfoDTO catagoryDTO = new MstcatagoryInfoDTO();
			catagoryDTO.setId(item.getId());
			catagoryDTO.setLevel(item.getLevel());
			catagoryDTO.setName(item.getName());
			catagoryDTO.setOrders(item.getOrders());
			catagoryDTO.setFatherid(item.getFatherid());
			catagoryArray.add(catagoryDTO);
		}
		return catagoryArray.toArray(new MstcatagoryInfoDTO[catagoryArray.size()]);
	}

	@Override
	public boolean deleteCatagory(Integer id) {
		MstcatagoryDTO mstcatagoryDTO = new MstcatagoryDTO();
		mstcatagoryDTO.setId(id);
		MstcatagoryDTO catagory = queryDAO.queryForObject(MstcatagoryDTO.EQUAL_SEARCH, mstcatagoryDTO);
		if (catagory == null) {
			return false;
		} else if (catagory.getFatherid() == null) {
			MstcourseDTO courseDTO = new MstcourseDTO();
			courseDTO.setcoursetypep(id);
			List<MstcourseDTO> courses = queryDAO.queryForObjectList(MstcourseDTO.EQUAL_SEARCH, courseDTO);
			if (courses.size() > 0) {
				return false;
			}
		} else {
			MstcourseDTO courseDTO = new MstcourseDTO();
			courseDTO.setcoursetypec(id);
			List<MstcourseDTO> courses = queryDAO.queryForObjectList(MstcourseDTO.EQUAL_SEARCH, courseDTO);
			if (courses.size() > 0) {
				return false;
			}
		}
		MstcatagoryDTO mstcatagoryDTO2 = new MstcatagoryDTO();
		mstcatagoryDTO2.setFatherid(id);
		List<MstcatagoryDTO> catagorys = queryDAO.queryForObjectList(MstcatagoryDTO.EQUAL_SEARCH, mstcatagoryDTO2);
		if (catagorys.size() > 0) {
			return false;
		}

		updateDAO.update(MstcatagoryDTO.DELETE, mstcatagoryDTO);
		return true;
	}

	@Override
	public boolean updateCatagory(Integer id, String name, Integer level, Integer orders, Integer fatherid) {
		MstcatagoryDTO catagoryDTO = new MstcatagoryDTO();

		catagoryDTO.setName(name);
		catagoryDTO.setFatherid(fatherid);
		List<MstcatagoryDTO> catagorys = queryDAO.queryForObjectList(MstcatagoryDTO.EQUAL_SEARCH, catagoryDTO);
		if ((catagorys.size() == 1 && catagorys.get(0).getId().equals(id)) || catagorys.size() == 0) {
			catagoryDTO.setLevel(level);
			catagoryDTO.setOrders(orders);
			catagoryDTO.setId(id);
			// 如果上级类别为null
			if (fatherid == null) {
				MstcatagoryDTO catagoryDTO2 = new MstcatagoryDTO();
				catagoryDTO2.setId(id);
				// 查询此类别
				MstcatagoryDTO catagory = queryDAO.queryForObject(MstcatagoryDTO.EQUAL_SEARCH, catagoryDTO2);
				// 此类别上级目录不为null(把二级目录变为一级目录)
				if (null != catagory && null != catagory.getFatherid()) {
					return false;

				} else {
					updateDAO.updateOne(MstcatagoryDTO.UPDATE, catagoryDTO, M10190WS, SH_INFO);
					return true;
				}
			} else {
				updateDAO.updateOne(MstcatagoryDTO.UPDATE, catagoryDTO, M10190WS, SH_INFO);
				return true;
			}

		} else {
			return false;
		}

	}

}
